<template>
	<view>

		<!--H5显示此项-->
		<!-- #ifdef H5 || APP-PLUS -->
		<view class="logobox">
			<image src="../../static/img/moren.png"></image>
			<view>填写应用名称</view>
		</view>
		<view class="inv-h-w">
			<view :class="['inv-h',Inv==0?'inv-h-se':'']" @click="Inv=0">司机免密登录</view>
			<view :class="['inv-h',Inv==1?'inv-h-se':'']" @click="Inv=1">会员登录</view>
		</view>
		<view class="bankcard" v-show="Inv == 0">
			<view class="input_box">
				<image src="../../static/img/zhangh.png"></image>
				<view>
					<input type="text" placeholder="请输入手机号" />
				</view>
			</view>
			<view class="input_box">
				<image src="../../static/img/mima.png"></image>
				<view class="input_item">
					<input type="text" placeholder="请输入验证码" />
				</view>
				<view class="yancode">获取验证码</view>
			</view>
		</view>
		<view class="bankcard" v-show="Inv == 1">
			<view class="input_box">
				<image src="../../static/img/zhangh.png"></image>
				<view>
					<input type="text" placeholder="请输入账号" />
				</view>
			</view>
			<view class="input_box">
				<image src="../../static/img/mima.png"></image>
				<view class="input_item">
					<input type="text" password="true" placeholder="请输入密码" />
				</view>
			</view>
		</view>
		<view class="login_btn">
			登录
		</view>
		<view class="newacc">注册新账号></view>
		<!-- #endif -->


		<!--微信小程序显示此项-->
		<!-- #ifdef MP-WEIXIN -->
		<view class="logobox2">
			<image src="../../static/img/moren.png"></image>
		</view>
		<!-- <view class="weixinapp">
				<view>《填写应用名称》申请获得以下权限</view>
				<view>获取您的公开信息(昵称,头像,手机等)</view>
			</view> -->
		<view class="login_btn" @click="weixinlogin">
			授权登录
		</view>
		<!-- #endif -->


		<view class="bottom_info">
			<view class="xieyi">
				<!-- 登录代表同意我们《<span>服务协议</span>》和《<span>隐私政策</span>》 -->
			</view>
			<view class="beian">
				Copyright © 2024 苏州众联智港信息科技有限公司
			</view>
		</view>
		<bindmobile ref="bindmobile" @getPhoneNumber="ongetPhoneNumber"></bindmobile>
	</view>
</template>

<script>
	import bindmobile from "../../components/bind-mobile.vue"
	export default {
		components: {
			bindmobile
		},
		data() {
			return {
				Inv: 0,
				session_key: "",
				userInfo: {},
				openid: "",
				avatarUrl: ''
			}
		},
		methods: {
			changeTab(Inv) {
				this.navIdx = Inv;
			},
			weixinlogin() {
				let that = this
				let authobj = {};

				uni.login({
					success(res) {
						authobj.code = res.code;
					}
				})

				uni.getUserProfile({
					desc: "desc",
					lang: 'zh_CN',
					success: function(res) {
						authobj.userInfo = res.rawData;
						that.avatarUrl = res.userInfo.avatarUrl
						uni.request({
							// url: 'https://api.zlkj-port.com/prod-api/user/auth?code=001',
							url: 'https://api.zlkj-port.com/prod-api/user/auth?code=001',
							data: {
								...authobj
							},
							success: (res) => {
								console.log('敷衍', res)
								that.userInfo = res.data.data  // 这边赋值才是正确的
								that.userInfo.avatarUrl = that.avatarUrl
								that.session_key = res.data.data.session_key
								that.openid = res.data.data.openid
								if (res.data.data.tel) {
									that.userInfo.mobile = res.data.data.tel;
									// that.userInfo.portName = res.data.data.portName
									that.userInfo.openid = that.openid;  
									uni.setStorageSync("userInfo", that.userInfo);
									uni.setStorageSync("nickName", res.data.data.nickName);
									uni.setStorageSync("portname", res.data.data.portroledata);
									
									that.$store.commit('loginSuccess', {
										userInfo: that.userInfo
									})

									uni.navigateBack({
										delta: 1
									})
									return;
								}else{
									that.$refs.bindmobile.open() 
									// that.regorLoginMember('', '')  // 姓名，车队名从哪里获取
								}
							}
						});
					},
					fail(err) {
						if (err.errMsg == "getUserProfile:fail auth deny") {
							uni.reLaunch({
								url: that.redirectUrl
							})
						}
					}
				})

				// let userWxId = ''
				// let that = this
				// uni.login({
				// 	provider: 'weixin',
				// 	success: loginRes => {
				// 		let code = loginRes.code
				// 		console.log(code)
				// 		this.$http.request(this.$httpApi.cloudPortUrl.getSession, {code: code})
				// 		.then((res) => {
				// 			let data = JSON.parse(res.data)
				// 			userWxId = data.openid
				// 			console.log('wxid is:' + userWxId)
				// 		})
				// 		.catch(err => {
				// 			console.log(err)
				// 		})
				// 	}
				// });
				// uni.getUserProfile({
				// 	desc: '登录',
				// 	lang:'zh_CN',
				// 	success: infoRes => {
				// 		console.log('用户信息是：')
				// 		console.log(infoRes)
				// 		that.regorLoginMember(userWxId, infoRes.userInfo.nickName, infoRes.userInfo.avatarUrl)
				// 	},
				// 	fail (res) {
				// 		console.log(res)
				// 	}
				// });
			},
			ongetPhoneNumber(e) {
				let that = this;
				// console.log(e.encryptedData) 
				// uni.showToast({
				// 	title: e.encryptedData,
				// 	icon: 'none'
				// })
				//that.regorLoginMember(that.openid, that.userInfo.nickName)
				// console.log(e) 
				uni.request({
					// url: 'https://api.zlkj-port.com/prod-api/user/auth/phone',   http://localhost:9021
					url: 'https://api.zlkj-port.com/prod-api/user/auth/phone1',
					data: {
						encryptedData: e.encryptedData,
						iv: e.iv,
						weixinname: that.userInfo.nickName,
						session_key: that.session_key,
						openid: that.openid
					},
					success: (res) => {
						// console.log(res.data)
						if (res.data.msg == "绑定成功！") {
							that.userInfo.mobile = res.data.phone;
							// that.userInfo.portName = res.data.portName
							// that.userInfo.roleName = res.data.roleName
							// uni.setStorageSync("userInfo", that.userInfo)
							// uni.setStorageSync("nickName", res.data.nickName)
							// uni.setStorageSync("portname", res.data.portroledata)
							that.$refs.bindmobile.close()
							
							that.$store.commit('loginSuccess', {
								userInfo: that.userInfo
							})
							uni.navigateBack({
								delta: 1
							})
							// 注册界面
							setTimeout(() => {
								uni.redirectTo({
									url: "/pages/login/reg?openid="+ this.openid + "&phone=" + res.data.phone
								})
							}, 500)
							
						}
						// that.$store.commit('loginSuccess', {
						// 	userInfo: that.userInfo
						// })
					}
				});
			},
			regorLoginMember(wxid, nickName) { // 检测是否存在用户，存在则直接登录，不存在则先注册再登录
				uni.navigateTo({
					url: "/pages/login/reg?wxid="+wxid+"&nickName="+nickName
				})
				// this.$http.request(this.$httpApi.cloudPortUrl.listMember, {
				// 		wxid: wxid
				// 	})
				// 	.then((res) => {
				// 		if (res && res.data && res.data.length > 0) { // 已存在，同步用户信息
				// 			this.$store.commit('loginSuccess', {
				// 				userInfo: res.data[0]
				// 			})
				// 			uni.navigateBack()
				// 		} else { // 不存在，转向信息补充页
				// 			uni.navigateTo({
				// 				url: '/pages/login/reg?wxid=${wxid}&nickName=${nickName}'
				// 			})
				// 		}
				// 	})
				// 	.catch(err => {
				// 		console.log(err)
				// 	})
			}
		}
	}
</script>

<style lang="scss">
	.logobox {
		text-align: center;
		font-size: 35rpx;
		color: #2C405A;
		font-weight: 600;
		margin-top: 100px;
	}

	.logobox2 {
		text-align: center;
		font-size: 35rpx;
		color: #2C405A;
		font-weight: 600;
		margin-top: 100px;
	}

	.logobox image {
		width: 150rpx;
		height: 150rpx;
		border-radius: 100px;
		margin-bottom: 10px;
	}

	.logobox2 image {
		width: 150rpx;
		height: 150rpx;
		border-radius: 100px;
		margin-bottom: 10px;
	}

	.inv-h-w {
		background-color: #FFFFFF;
		display: flex;
		align-items: center;
		width: 65%;
		margin: 0 auto;
		margin-top: 50px;
		margin-bottom: 30px;
	}

	.inv-h {
		font-size: 30upx;
		flex: 1;
		text-align: center;
		color: #353535;
		height: 100upx;
		line-height: 100upx;
	}

	.inv-h-se {
		color: #007aff;
		border-bottom: 4upx solid #007aff;
	}

	.binding_container {
		top: 0;
		right: 0;
		width: 100%;
		height: 100%;
		background-color: #fff;
	}

	.binding_methods /deep/ .van-tab__text {
		font-size: 0.4rem;
		color: #1e1f20;
		font-weight: 400;
		line-height: 0.56rem;
	}

	.bankcard {
		padding: 0.43rem;
		width: 90%;
		margin: 0 auto;
	}

	.input_box {
		display: flex;
		align-items: center;
		border-bottom: 1px solid #a2a2a2;
		height: 58px;
	}

	.input_box image {
		width: 50rpx;
		height: 50rpx;
		margin-right: 15px;
	}

	.yancode {
		border: 1px solid #2C405A;
		border-radius: 4px;
		padding: 8px 8px;
		margin-left: auto;
	}

	.input_item {
		width: 50%;
	}

	.login_btn {
		width: 90%;
		background-color: #007AFF;
		color: #FFFFFF;
		height: 100rpx;
		line-height: 100rpx;
		text-align: center;
		border-radius: 10px;
		margin: 15px auto;
	}

	.newacc {
		text-align: center;
		color: #82848A;
	}

	.xieyi {
		text-align: center;
		color: #808080;
		margin-top: 50px;
	}

	.xieyi span {
		color: #0070FF;
	}

	.beian {
		text-align: center;
		margin-top: 3px;
	}

	.bottom_info {
		margin: 30px auto;
		position: absolute;
		bottom: 2px;
		left: 0;
		right: 0;
	}

	.weixinapp {
		margin: 30px auto;
		text-align: center;
	}

	.weixinapp view:nth-child(2) {
		color: #555555;
	}
</style>
